//引入代码
var call = require('../../util/request.js');
var timeps = require('../../util/timestamp.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    widHeight:0,
    currentTab: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },
  onShow(){
	//全部门票
	this.orderAllListp();  
	
	//待付款
	this.orderRefundp();
	
	//待使用
	this.used();
	
	//已使用
	this.beenused();
	
	//退款
	this.order2List()
  },
  
  orderAllListp:function(){
  	  //全部数据
  	call.getData('api/orderAllList?page=' + 1 +'&size='+20 +'&openid='+tt.getStorageSync("key"), this.orderAllList, this.fail);
  },
  orderRefundp:function(){
	  //待付款
	   call.getData('api/orderList?page=' + 1 +'&size='+20 +'&openid='+tt.getStorageSync("key") +'&order_status='+0, this.orderLists, this.fail);
  },
  used:function(){
  	//待使用
  	   call.getData('api/orderList?page=' + 1 +'&size='+20 +'&openid='+tt.getStorageSync("key") +'&order_status='+2, this.useds, this.fail);
  },
  beenused:function(){
  	 //已使用
  	  call.getData('api/orderList?page=' + 1 +'&size='+20 +'&openid='+tt.getStorageSync("key") +'&order_status='+6, this.beenuseds, this.fail);
  },
  order2List:function(){
  	  //退款
  	  call.getData('api/order2List?page=' + 1 +'&size='+20 +'&openid='+tt.getStorageSync("key"), this.saleLists, this.fail);
  },
  
  //全部的订单数据
  orderAllList:function(data){
  	   var that = this;
  	   var recieverArr=[];
  	//  console.log('订房'+JSON.stringify(data.data))
  	data.data.data.forEach((item) => {
  		var recieverObj = {
  			order_num : item.order_num,
  			order_title: item.order_title,
  			pay_exprire_time: timeps.formatTimeTwo(item.pay_exprire_time*1000, 'Y-M-D'),
  			pay_fee:item.pay_fee,
  			order_status:item.order_status,
  			id:item.id,
  			photo:item.photo,
  			gt_id:item.gt_id
  		}
  		console.log('使用'+JSON.stringify(recieverObj))
  		recieverArr.push(recieverObj);
  		
  		//console.log('征信地址'+JSON.stringify(recieverArr))
  		that.setData({
  			order: recieverArr
  		})
  	})
  	
  	  var query = tt.createSelectorQuery();
  	  var that = this;
  	  /**
  	   * 获取系统信息
  	   */
  	  var zhang = this.data.order.length;
  	  query.select('.main').boundingClientRect(function (rect) {
  	    that.setData({
  	      // 获取要循环标签的高度
  	      height: 300 ,
  	      widHeight: 300 * zhang + (zhang*17) + "px"
  	    })
  	  }).exec();
	
  	  
  },
  //待付款页面订单回调
  orderLists:function(data){
  	  var that = this;
  	    var recieverArr=[];
  	  console.log('待付款回调'+JSON.stringify(data.data.data))
  	  if(data.data.data==''){
		  console.log('空的')
  		  that.setData({
  		  	orderList: recieverArr,
  		  })
		  console.log('数字'+this.data.orderList.length)
  	  }else{
  		
  		//  console.log('订房'+JSON.stringify(data.data))
  		data.data.data.forEach((item) => {
  			var recieverObj = {
  				order_num : item.order_num,
  				order_title: item.order_title,
  				pay_exprire_time: timeps.formatTimeTwo(item.pay_exprire_time*1000, 'Y-M-D'),
  				pay_fee:item.pay_fee,
  				order_status:item.order_status,
  				id:item.id,
  				photo:item.photo,
  				gt_id:item.gt_id
  			}
  			console.log('待付款'+JSON.stringify(recieverObj))
  			recieverArr.push(recieverObj);
  			
  			console.log('征信地址'+JSON.stringify(recieverArr))
  				
  			that.setData({
  				orderList: recieverArr
  			})
  		})
  		  
  	  }
  	  
  },
  useds:function(data){
  	//待使用
  	var that = this;
  	var recieverArr=[];
  	if(data.data.data==''){
  			  that.setData({
  			  	orderuseds: recieverArr
  			  })
  	}else{
  			
  			//  console.log('订房'+JSON.stringify(data.data))
  			data.data.data.forEach((item) => {
  				var recieverObj = {
  					order_num : item.order_num,
  					order_title: item.order_title,
  					pay_exprire_time: timeps.formatTimeTwo(item.pay_exprire_time*1000, 'Y-M-D'),
  					pay_fee:item.pay_fee,
  					order_status:item.order_status,
  					id:item.id,
  					photo:item.photo,
  					gt_id:item.gt_id
  				}
  				recieverArr.push(recieverObj);
  				
  					
  				that.setData({
  					orderuseds: recieverArr
  				})
  			})
  			  
  	}
  	
  },	
  beenuseds:function(data){
  	//已使用
  	console.log('已使用'+data)
  	var that = this;
  	var recieverArr=[];
  	if(data.data.data==''){
  			  that.setData({
  			  	orderbeen: recieverArr
  			  })
  	}else{
  			//  console.log('订房'+JSON.stringify(data.data))
  			data.data.data.forEach((item) => {
  				var recieverObj = {
  					order_num : item.order_num,
  					order_title: item.order_title,
  					pay_exprire_time: timeps.formatTimeTwo(item.pay_exprire_time*1000, 'Y-M-D'),
  					pay_fee:item.pay_fee,
  					order_status:item.order_status,
  					id:item.id,
  					photo:item.photo,
  					gt_id:item.gt_id
  				}
  				recieverArr.push(recieverObj);
  				
  					
  				that.setData({
  					orderbeen: recieverArr
  				})
  			})
  			  
  	}
  },
  
  saleLists:function(data){
  	//售后退款
  	console.log('已使用'+data)
  	var that = this;
  	var recieverArr=[];
  	if(data.data.data==''){
  			  that.setData({
  			  	ordersale: recieverArr
  			  })
  	}else{
  			
  			//  console.log('订房'+JSON.stringify(data.data))
  			data.data.data.forEach((item) => {
  				var recieverObj = {
  					order_num : item.order_num,
  					order_title: item.order_title,
  					pay_exprire_time: timeps.formatTimeTwo(item.pay_exprire_time*1000, 'Y-M-D'),
  					pay_fee:item.pay_fee,
  					order_status:item.order_status,
  					id:item.id,
  					photo:item.photo,
  					gt_id:item.gt_id
  				}
  				recieverArr.push(recieverObj);
  				
  				that.setData({
  					ordersale: recieverArr
  				})
  			})
  			  
  	}
  },
  
  
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    // 获取单个轮播循环的高度
    var heights=this.data.height;
    // 获取一级成员的数组个数
    var zhang = this.data.order.length
    // 获取二级成员的数组个数
	//待付款
   // var orderLists = this.data.orderList.length
	
    console.log(heights)
    that.setData({
      currentTab: e.detail.current
    });
    if (this.data.currentTab==0){
      that.setData({
        widHeight: heights * zhang+ (zhang*17) + "px"
      });
   
    }else if(this.data.currentTab==1){
		
	that.orderRefundp()
	
	if(this.data.orderList.length==0){
		that.setData({
		    widHeight: 100 + "px"
		  });
	}else{
		that.setData({
		    widHeight: heights * this.data.orderList.length + (this.data.orderList.length*17)+"px"
		  });
	}
   
    }else if(this.data.currentTab==2){
		that.used()
		if(this.data.orderuseds.length==0){
			that.setData({
			    widHeight: 100 + "px"
			  });
		}else{
			that.setData({
			  widHeight: heights * this.data.orderuseds.length + (this.data.orderuseds.length*17) + "px"
			});
		}
		
	}else if(this.data.currentTab==3){
		that.beenused()
		if(this.data.orderbeen.length==0){
			that.setData({
			    widHeight: 100 + "px"
			  });
		}else{
			that.setData({
			  widHeight: heights * this.data.orderbeen.length + (this.data.orderbeen.length*17) + "px"
			});
		}
	}else if(this.data.currentTab==4){
		this.order2List()
		if(this.data.ordersale.length==0){
			that.setData({
			    widHeight: 100 + "px"
			  });
		}else{
			that.setData({
			  widHeight: heights * this.data.ordersale.length + (this.data.ordersale.length*17) +"px"
			});
		}
	}
    // console.log(e.detail.current);
  },
  
  
  //点击切换
  clickTab: function(e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  
  
  //进入详情
  order_details:function(e){
  	  console.log('事件'+e.currentTarget.dataset.id)
  	  
  	  tt.navigateTo({
  	  	url: '/pages/order_details/order_details?id='+e.currentTarget.dataset.id,
  	  })
  },
  //申请退款
  refunds:function(e){
  	    tt.navigateTo({ url: '/pages/application/application?order_id='+e.currentTarget.dataset.id })
  	   // var postData = {
  		  // order_id:e.currentTarget.dataset.id
  	   // }
  	   // call.request('api/orderRefund',postData,this.orderRefund, this.fail);
  },
  
  orderRefund:function(data){
  	  console.log(data)
  	  tt.showToast({
  	  	icon: 'none',
  	  	title: data.msg
  	  })
  },
  
  //删除订单
  orderDel:function(e){
  	  
  		console.log('回调'+e.currentTarget.dataset.ids)
  	    call.delData('api/orderDel?order_id='+e.currentTarget.dataset.ids,this.orderDels, this.fail);
  },
  
  orderDels:function(data){
	  tt.showToast({
	    title: '亲,删除成功',
	  });
	  this.used();
  	  this.orderAllListp();
	  this.order2List();
  	  console.log('删除的回调'+JSON.stringify(data))
  },
  
  //立即付款
  payment:function(e){
  	  console.log('立即付款id'+e.currentTarget.dataset.id)
  	  var postData={
  		  order_id:e.currentTarget.dataset.id
  	  }
	  
	  
  	 /* call.request('api/orderPay',postData,this.orderPay, this.fail); */
  	  
	  tt.request({
	    url: "https://dy.nbhysj.com/api/orderPay",
	    data: {
	  	 order_id:e.currentTarget.dataset.id
	    },
	    method: 'POST',
	    success(data) {
	  	console.log('进来了吗'+JSON.stringify(data.data.data))
	  	tt.pay({
	  	  orderInfo: data.data.data,
	  	  service: 1,
	  	  getOrderStatus(res) {
	  		
	  	      let { out_order_no } = res;
	  	      return new Promise(function (resolve, reject) {
	  		
	  			tt.request({
	  			  url: "https://dy.nbhysj.com/api/getOrderStatus",
	  			  data: {out_order_no:out_order_no},
	  			  method: 'POST',
	  			  success(res) {
	  				 // 商户后端查询的微信支付状态，通知收银台支付结果
	  				  console.log('经来么'+res.data.data.order_status)
	  				 if(res.data.data.order_status==2){
	  				
	  					 resolve({code:0});
	  					
	  				 }
	  			  },
	  			  fail(err) {
	  				reject(err);
	  			  }
	  			});  
	  			
	  	      });
	  	    },
	  	  success(res) {
	  		  console.log('支付状态查询哈哈'+res.code)
	  		if(res.code==0){
	  			var postData = {
	  					out_order_no:data.data.out_order_no
	  				}
	  				tt.request({
	  				  url: "https://dy.nbhysj.com/api/getOrderStatus",
	  				  data: postData,
	  				  method: 'POST',
	  				  success(res) {
	  					
	  					 // 商户后端查询的微信支付状态，通知收银台支付结果
	  					 if(res.data.data.order_status==2){
	  						 tt.navigateTo({
	  						 	url: '/pages/verification/verification?order_no='+res.data.data.id
	  						 })
	  					 }
	  				  },
	  				  fail(err) {
	  					reject(err);
	  				  }
	  				});
	  		}  
	  		
	  			
	  		
	  	
	  	  },
	  	   fail(res) {
	  	    
	  	    },
	  	});
	    },
	    fail(err) {
	  	reject(err);
	    }
	  });  
  },
/*  orderPay:function(data){
  	console.log('立即付款回调'+JSON.stringify(data))
  	
  
  	console.log('结算时间'+ data.data.data)
  	tt.pay({
  	  orderInfo: data.data.data,
  	  service: 1,
  	  getOrderStatus(res) {
  		  console.log('商户是的'+JSON.stringify(res))
  	      let { out_order_no } = res;
  	      return new Promise(function (resolve, reject) {
  			console.log('请求借口')  
  			
  			tt.request({
  			  url: "https://dy.nbhysj.com/api/getOrderStatus",
  			  data: {out_order_no:out_order_no},
  			  method: 'POST',
  			  success(res) {
  				 console.log('经来么'+res.data.data.order_status)
  				 if(res.data.data.order_status==2){
  				
  					 resolve({code:0});
  					
  				 }
  			  },
  			  fail(err) {
  				reject(err);
  			  }
  			});  
  			
  			
  			
  	      });
  	    },
  	  success(res) {
  		  console.log('支付状态查询哈哈'+res.code)
  		if(res.code==0){
  			var postData = {
  					out_order_no:data.data.out_order_no
  				}
  				tt.request({
  				  url: "https://dy.nbhysj.com/api/getOrderStatus",
  				  data: postData,
  				  method: 'POST',
  				  success(res) {
  					  console.log('查询到了吗')
  					  console.log('后面的商户状态'+JSON.stringify(res.data.data.order_status))
  					
  					 if(res.data.data.order_status==2){
  						 tt.navigateTo({
  						 	url: '/pages/verification/verification?order_no='+res.data.data.id
  						 })
  					 }
  				  },
  				  fail(err) {
  					reject(err);
  				  }
  				});
  		}  
  		
  			
  		
  	
  	  },
  	   fail(res) {
  	    
  	    },
  	});
  	
  }, */
  
  //取消订单
  orderRefund:function(e){
  	  call.getData('api/orderChannel?order_id='+e.currentTarget.dataset.id,this.orderRefunds, this.fail);
  },
  
  orderRefunds:function(e){
  	  // this.setData({
  	  //   currentTab: 0
  	  // })
	  tt.showToast({
	    title: '亲,取消订单成功',
	  });
  	  this.orderAllListp()
  	  this.orderRefundp()
  	  
  	  
  	  
  },
  
  
  //再次预订
  again:function(e){
  	  console.log('id'+e.currentTarget.dataset.id)
  	 
  	   tt.navigateTo({
  	   	url: '/pages/order/order?id='+e.currentTarget.dataset.id + '&name='+JSON.stringify(e.currentTarget.dataset.title),
  	   })
  }
  
  
  })